<?php

namespace addons\Dining\common\models;

use Yii;

/**
 * This is the model class for table "{{%addon_oa_dining}}".
 *
 * @property int $id ID
 * @property int $type 类型
 * @property int $num 人数
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 */
class Dining extends \common\models\base\BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_dining}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cate_id', 'num'], 'required'],
            [['merchant_id','member_id','cate_id', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
            [['date'], 'safe'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '用户',
            'cate_id' => '就餐类型',
            'num' => '就餐人数',
            'date' => '就餐时间',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    /**
     * 关联用户
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMember()
    {
        return $this->hasOne(\common\models\merchant\Member::class, ['id' => 'member_id']);
    }
    public function beforeSave($insert)
    {
        if(Yii::$app->id == 'backend'||Yii::$app->id == 'merchant')
            $this->member_id = Yii::$app->user->id;
        else
            $this->member_id = Yii::$app->user->identity->member_id;
        $this->merchant_id = Yii::$app->user->identity->merchant_id;

        return parent::beforeSave($insert);

    }
}